package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.controller.ZnxController;
import com.example.entity.hz.Znx;
import com.example.entity.yifei.Moctm;
import com.example.entity.yifei.Moctn;
import com.example.entity.yifei.Purtl;
import com.example.entity.yifei.Purtm;
import com.example.exception.BaseException;
import com.example.exception.CustomException;
import com.example.mapper.MoctnMapper;
import com.example.service.IActFlowCustomService;
import com.example.service.MoctmService;
import com.example.mapper.MoctmMapper;
import com.example.untils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author 14501
* @description 针对表【MOCTM】的数据库操作Service实现
* @createDate 2024-08-05 14:43:13
*/
@Service
public class MoctmServiceImpl extends ServiceImpl<MoctmMapper, Moctm> implements MoctmService, IActFlowCustomService {

    @Resource
    private MoctmMapper moctmMapper;
    @Resource
    private MoctnMapper moctnMapper;

    @Override
    public Boolean tc30IsN(String tc001, String tc002) {
        LambdaQueryWrapper<Moctm> queryWrapper= new LambdaQueryWrapper<Moctm>();
        queryWrapper.eq(Moctm::getTM001,tc001).eq(Moctm::getTM002,tc002);
        Moctm moctm = moctmMapper.selectOne(queryWrapper);
        if (moctm!=null&&moctm.getTM012().equals("0")){
            return true;
        }else {
            throw new BaseException(tc001+"-"+tc002+"该委外核价单不是未审核委外核价单");
        }
    }

    @Override
    public Map<String, Object> setvariables(String id, String userName) {
        Map variables=new HashMap<>();
        variables.put("assignee0",userName);
        variables.put("processInsName","核价单流程");
        return variables;
    }

    @Override
    public Map<String, Object> setvariables(String id, String userName, Object vo) {
        return null;
    }

    @Override
    public void startRunTask(String id) {

    }
    @Autowired
    private ZnxController znxController;

    @Override
    public void endRunTask(String id, Boolean state, String defineId, String initiator) {
        System.out.printf("委外核价单审批流程结束业务操作！！！！id="+id+"审核状态为："+state);
        String[] strings = id.split(":");
        Moctm moctm = moctmMapper.selectOne(new LambdaQueryWrapper<Moctm>().eq(Moctm::getTM001, strings[0]).eq(Moctm::getTM002, strings[1]));
        if(moctm == null) {
            throw new CustomException("500", "委外核价单不存在");
        }
        if(state) {  // 审核通过
            moctm.setTM012("3");
            moctmMapper.update(moctm, new LambdaQueryWrapper<Moctm>().eq(Moctm::getTM001, strings[0]).eq(Moctm::getTM002, strings[1]));
            checkmoctm(strings[0], strings[1]);
            List<Znx> znxList=new ArrayList<>();
            Znx znx=Znx.builder().code(id).content("有一张新的请购单通过了审核!").departmentCode("90802").notificationCode(initiator).customField(defineId)
                    .title("通知").build();
            znxList.add(znx);
            znxController.insert(znxList);
        }else{  // 审批失败
            moctm.setTM012("2");
            moctmMapper.update(moctm,new LambdaQueryWrapper<Moctm>().eq(Moctm::getTM001, strings[0]).eq(Moctm::getTM002, strings[1]));
        }
    }

    public void checkmoctm(String TA001, String TA002) {
        if(StringUtils.isEmpty(TA001) || StringUtils.isEmpty(TA002)) {
            throw new CustomException("500", "单别或单号为空");
        }
        Moctm moctm = moctmMapper.selectOne(new LambdaQueryWrapper<Moctm>().eq(Moctm::getTM001,TA001).eq(Moctm::getTM002,TA002));
        if(moctm == null) {
            throw new CustomException("500", "单据不存在!");
        }
        moctm.setTM009("Y");
        moctmMapper.update(moctm, new LambdaQueryWrapper<Moctm>().eq(Moctm::getTM001,TA001).eq(Moctm::getTM002,TA002));
        Moctn moctn = new Moctn();
        moctn.setTN014("Y");
        moctnMapper.update(moctn, new LambdaQueryWrapper<Moctn>().eq(Moctn::getTN001, TA001).eq(Moctn::getTN002, TA002));
    }

    @Override
    public Object taskInfo(Map<String, Object> processVariables) {
        return processVariables.get("object");
    }
}




